package scau.mis4.chanyulin.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.entity.PriceHistory;
import scau.mis4.chanyulin.service.IPriceHistoryService;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * PriceHistory Controller层
 */
@RestController
@RequestMapping("/pricehistory")
public class PriceHistoryController extends BaseController {

    @Autowired
    private IPriceHistoryService priceHistoryService;

    /**
     * 添加PriceHistory
     * @param priceHistory
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public String addPriceHistory(PriceHistory priceHistory) throws Exception {
        priceHistoryService.add(priceHistory);
        return createSuccessJSON();
    }

    /**
     * 删除某个PriceHistory
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public String deletePriceHistoryById(String id) throws Exception {
        priceHistoryService.delete(id);
        return createSuccessJSON();
    }

    /**
     * 查找某个PriceHistory
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get")
    public String getPriceHistoryById(String id) throws Exception {
        PriceHistory priceHistory = priceHistoryService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", priceHistory);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部PriceHistory
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllPriceHistory() throws Exception {
        List<PriceHistory> priceHistoryList = priceHistoryService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result",priceHistoryList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询PriceHistory
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listPriceHistoryByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<PriceHistory> priceHistoryList = priceHistoryService.findAll();
        PageInfo<PriceHistory> pageInfo = new PageInfo<>(priceHistoryList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", priceHistoryList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个PriceHistory
     * @param priceHistory
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public String updatePriceHistoryById(PriceHistory priceHistory) throws Exception {
        priceHistoryService.updateById(priceHistory);
        return createSuccessJSON();
    }

}
